# 全局用户数据（模拟数据库，每个用户是一个字典）
g_users = [
    {"id": 1, "username": "Alice", "password": "123", "money": 5000.0},
    {"id": 2, "username": "Bob", "password": "456", "money": 3000.0}
]
# 当前登录用户（初始为空）
g_loginuser = None

def display_user(user):
    """显示用户余额"""
    print(f"用户: {user['username']}, 余额: {user['money']:.2f}")

def withdraw():
    """取款功能"""
    global g_loginuser
    if not g_loginuser:
        print("未登录，无法取款！")
        return
    
    try:
        amount = float(input("请输入取款金额: "))
        if amount <= 0:
            print("金额必须大于0！")
            return
        if amount <= g_loginuser["money"]:
            g_loginuser["money"] -= amount
            print(f"取款成功！当前余额: {g_loginuser['money']:.2f}")
        else:
            print("余额不足，取款失败！")
    except ValueError:
        print("输入无效，请输入数字！")


def main():
    """主程序：处理登录和主菜单"""
    global g_loginuser
    
    # 1. 登录验证
    print("=" * 20)
    print("欢迎使用简易ATM系统")
    print("=" * 20)
    
    try:
        uid = int(input("请输入ID: "))
        pwd = input("请输入密码: ")
    except ValueError:
        print("ID必须是数字！")
        return
    
    # 遍历用户列表验证登录
    logged_in = False
    for user in g_users:
        if user["id"] == uid and user["password"] == pwd:
            g_loginuser = user.copy()  # 拷贝用户数据（避免修改全局原始数据）
            print("登录成功！")
            logged_in = True
            break
    
    if not logged_in:
        print("ID或密码错误，退出程序！")
        return
    
    # 2. 主菜单循环
    while True:
        print("" + "=" * 20)
        print("1. 查询余额  2. 取款  3. 退出")
        print("=" * 20)
        choice = input("请选择操作: ")
        
        if choice == "1":
            display_user(g_loginuser)
        elif choice == "2":
            withdraw()
        elif choice == "3":
            print("感谢使用，再见！")
            break
        else:
            print("无效选择，请重新输入！")


if __name__ == "__main__":
    main()
